package com.example.nightmeel;

import com.google.android.gcm.GCMRegistrar;
import com.navdrawer.SimpleSideDrawer;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

/**
 *
 */
public class TimeLineActivity extends Activity implements ItemListFragment.Callbacks {

    private static final String tag = TimeLineActivity.class.getSimpleName();
    private Activity self = this;
    AsyncTask<Void, Void, Void> mRegisterTask;

    private SimpleSideDrawer mSlidingMenu;

    private boolean launch = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        launch = true;
        // 初期処理
        init();
    }

    @Override
    public void onResume() {
        super.onResume();
        /*
        Log.d(tag, "onResume start.");
        if (launch) {
            launch = false;
        } else {
            makePublicTimeLine();
        }
        }*/
    }

    /**
     * Callback method from {@link ItemListFragment.Callbacks} indicating that
     * the item with the given ID was selected.
     */
    @Override
    public void onItemSelected(int id) {
        Log.d(tag, "onItemSelected start.");
        Log.d(tag, "id is " + id);
    }

    @Override
    public boolean onCreateOptionsMenu (Menu menu){
        getMenuInflater().inflate(R.menu.tl_menu, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        Log.d("ItemListActivity", "onMenuItemSelected start.");
        if (android.R.id.home == item.getItemId()) {
            mSlidingMenu.toggleLeftDrawer();
        } else if (R.id.menu_add == item.getItemId()) {
            Log.d(tag, "clicked add.");
            // Post画面を起動
            Intent intent = new Intent(this, ItemPostActivity.class);
            startActivity(intent);
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * 初期化処理.
     */
    private void init() {
        // 初期処理
        GCMRegistrar.checkDevice(this);
        GCMRegistrar.checkManifest(this);
        final String regId = GCMRegistrar.getRegistrationId(this);
        if (regId.equals("")) {
            // Automatically registers application on startup.
            GCMRegistrar.register(this, GCMIntentService.SENDER_ID);
        } else {
            // Device is already registered on GCM, check server.
            if (!GCMRegistrar.isRegisteredOnServer(this)) {
                final Context context = this;
                mRegisterTask = new AsyncTask<Void, Void, Void>() {

                    @Override
                    protected Void doInBackground(Void... params) {
                        boolean registered =
                                GCMIntentService.register(context, regId);
                        if (!registered) {
                            GCMRegistrar.unregister(context);
                        }
                        return null;
                    }

                    @Override
                    protected void onPostExecute(Void result) {
                        mRegisterTask = null;
                    }
                };
                mRegisterTask.execute(null, null, null);
            }
        }
        
        getActionBar().setHomeButtonEnabled(true);
        setContentView(R.layout.activity_item_list);

        mSlidingMenu = new SimpleSideDrawer(this);
        mSlidingMenu.setLeftBehindContentView(R.layout.behind_menu);
    }

}
